package com.lc.ibps.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lc.ibps.api.base.entity.WebAPIResult;
import com.lc.ibps.api.base.page.Page;
import com.lc.ibps.api.base.query.QueryFilter;
import com.lc.ibps.base.core.util.JacksonUtil;
import com.lc.ibps.base.db.model.DefaultQueryFilter;
import com.lc.ibps.base.db.mybatis.domain.DefaultPage;
import com.lc.ibps.base.framework.helper.MapBuilder;
import com.lc.ibps.base.framework.page.PageResult;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.cloud.provider.GenericProvider;

/**
 *  API base  Controller 类。
 *
 * <pre> 
 * 构建组：ibps-platform-appapi
 * 作者：hugh zhuang
 * 邮箱：3378340995@qq.com
 * 日期：2018年8月9日-上午11:20:36
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
public class BaseApiController extends GenericProvider{

	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	protected static final int SUCCESS = WebAPIResult.SUCCESS;
	protected static final int WARN = WebAPIResult.WARN;
	protected static final int FAIL = WebAPIResult.FAIL;
	protected static final int ERROR = WebAPIResult.ERROR;
	
	/**
	 * 和buildMapBuilder同义，缩写。
	 * 
	 * @return MapBuilder
	 */
	protected MapBuilder b() {
		return new MapBuilder();
	}

	/**
	 * 获取分页
	 *
	 * @param page
	 * @param limit
	 * @return
	 */
	public Page getPage(int page, int limit) {
		return new DefaultPage(page, limit);
	}

	/**
	 * 获取分页
	 *
	 * @param page
	 * @param limit
	 * @return
	 */
	public Page getPage(String page, String limit) {
		return new DefaultPage(Integer.parseInt(page), Integer.parseInt(limit));
	}
	
	/**
	 * 
	 * 获取queryFilter
	 *
	 * @param page
	 * @param limit
	 * @return
	 */
	public QueryFilter getQuerFilter(String page, String limit){
		DefaultQueryFilter queryFilter = new DefaultQueryFilter();
		Page pg = getPage(page, limit);
		queryFilter.setPage(pg);
		return queryFilter;
	}
	
	/**
	 * 
	 * 获取queryFilter
	 *
	 * @param page
	 * @param limit
	 * @return
	 */
	public QueryFilter getQuerFilter(){
		DefaultQueryFilter queryFilter = new DefaultQueryFilter();
		queryFilter.setPage(null);
		return queryFilter;
	}

	/**
	 * 获取分页返回结果
	 *
	 * @param result
	 * @param list
	 * @return
	 */
	public WebAPIResult getPageResult(WebAPIResult result, List<?> list) {
		result.addVariable("size", list.size());
		result.setData(list);
		return result;
	}
	
	/**
	 * 
	 * 获取分页返回结果
	 *
	 * @param result
	 * @param listData
	 * @param clazz
	 * @return
	 */
	public WebAPIResult getPageResult(WebAPIResult result, String listData, Class<?> clazz) {
		List<?> list = JacksonUtil.getDTOList(JacksonUtil.getString(listData, "data"), clazz);
		PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
		result.addVariable("pageResult", pageResult);
		result.setData(list);
		result.setResult(WebAPIResult.SUCCESS);
		return result;
	}

	/**
	 * 获取异常返回结果
	 *
	 * @param result
	 * @param e
	 * @return
	 */
	public WebAPIResult getExceptionResult(WebAPIResult result, Exception e) {
		//记录异常
		logger.error(e.getMessage(), e);
		result.setResult(WebAPIResult.ERROR);
		result.setMessage(e.getMessage());
		return result;
	}
	
	/**
	 * 
	 * 获取异常返回结果
	 *
	 * @param result
	 * @param message
	 * @param e
	 * @return
	 */
	public WebAPIResult getExceptionResult(WebAPIResult result, String message, Exception e) {
		//记录异常
		logger.error(e.getMessage(), e);
		result.setResult(WebAPIResult.ERROR);
		result.setMessage(message);
		result.setCause(e.getMessage());
		return result;
	}
	
	/**
	 * 
	 * 获取异常返回结果
	 *
	 * @param result
	 * @param state
	 * @param message
	 * @param e
	 * @return
	 */
	public <T> APIResult<T> getExceptionResult(APIResult<T> result, int state, String message, Exception e) {
		//记录异常
		logger.error(e.getMessage(), e);
		result.setState(state);
		result.setMessage(message);
		result.setCause(e.getMessage());
		return result;
	}
	
	/**
	 * 
	 * 获取返回结果
	 *
	 * @param result
	 * @param state
	 * @param message
	 * @return
	 */
	public <T> APIResult<T> getResult(APIResult<T> result, int state, String message) {
		result.setState(state);
		result.setMessage(message);
		return result;
	}
	
	/**
	 * 
	 * 获取返回结果
	 *
	 * @param result
	 * @param state
	 * @param message
	 * @param data
	 * @return
	 */
	public <T> APIResult<T> getResult(APIResult<T> result, int state, String message, T data) {
		result.setState(state);
		result.setMessage(message);
		result.setData(data);
		return result;
	}
}
